package com.google.codejam.alienlanguage;

import com.google.codejam.core.test.ACodeJamTestCase;
import com.google.codejam.core.test.ACodeJamTestSuiteRunner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Concrete implementation of the <code>ACodeJamTestSuiteRunner</code> for the
 * "Alien Language" problem.
 */
public class AlienLanguageTestSuiteRunner extends ACodeJamTestSuiteRunner
{
	protected List<String> list_of_words;
	protected int          number_of_words;
	protected int          length_of_words;

	/**
	 * Constructor that assigns the test suite name.
	 *
	 * @param test_suite_name
	 * 		  <code>String</code> containing the name of the test suite.
	 */
	public AlienLanguageTestSuiteRunner(String test_suite_name)
	{
		super(test_suite_name);
	}

	/**
	 * This method uses the input parameters (<code>String]\</code> args) to
	 * initialize this test suite runner instance.
	 * <p>
	 *    The following actions are performed to facilitate initialization:
	 *    <ol>
	 *       <li>Initialize input parameters - <code>initInputParams</code> method.</li>
	 *       <li>Initialize input and output - <code>initIO</code> method.</li>
	 *       <li>Initialize list of test cases - <code>initTestCaseList</code> method.</li>
	 *       <li>Initialize the list of words - <code>initListOfWords</code></li>
	 *    </ol>
	 * </p>
	 *
	 * @param args <code>String[]</code> containing the parameters used for
	 * initialization.
	 * @throws IOException
	 */
	@Override
	protected void init(String[] args)
			  throws IOException
	{
		initInputParams(args);
		initIO();
		initTestCaseList();
		initListOfWords();
	}

	@Override
	protected void initTestCaseList()
			  throws IOException
	{
		String[] first_line_params = input_reader.readLine().split(" ");

		length_of_words = Integer.parseInt(first_line_params[0]);
		number_of_words = Integer.parseInt(first_line_params[1]);
		test_case_count = Integer.parseInt(first_line_params[2]);

		test_case_list = new ArrayList<ACodeJamTestCase>(test_case_count);
	}

	@Override
	protected ACodeJamTestCase initNextTestCaseFromInput()
			  throws IOException
	{
		ACodeJamTestCase test_case = new AlienLanguageTestCase(list_of_words);
		test_case.parseDataString(input_reader.readLine());

		return test_case;
	}

	/**
	 * Initialize the list of words to be passed to each test case. The words are
	 * read from the input and added to the local member list.
	 */
	protected void initListOfWords()
		throws IOException
	{
		list_of_words = new ArrayList<String>(number_of_words);

		for (int i = 0; i < number_of_words; i++)
		{
			list_of_words.add(input_reader.readLine());
		}
	}
}
